﻿<?php
/* comments-->xml operation
 *
 * @link http://....
 * @copyright 2005  ling de ming
 * @author ivan <ldmmyx@hotmail.com>
 * @version 1.0
 *
 *  function --> comment(x.y: --->x same module,y is a funtion in same module)
 *	
 *  1.1  funCreateXml($filename,$root,$code='gb2312')			//---> create XML file
 *  2.1  funAddElement($name,$attribute,$value,$location='')	//---> add element
 */
 
define("ENTER",chr(13).chr(10));	//--- enter
class Xml
{
	private $data = array();		//--- save elements in XML file(element: element name, value: element value，attribute:（string include all attributs），location:，tag position，value is top that position is in top，there isn't the end tag, footer is the end tag，add the end tag undefine in the end tag
	
	/* 
	 *
	 */	
	public function __construct()
	{
	}

	/* create XML file
	 * @param  string filename      ← XML file name											
	 * @param  string root  		← XML root element						
	 * @param  string code   		← XML decode(default utf-8)									
	 */	
	function createXml($filename, $root, $rootAttribute, $code='utf-8')
	{
		$strtmp  = '<?xml version="1.0" encoding="'.$code.'"?>'.ENTER;	// document type
		$strtmp .= "<$root$rootAttribute>".ENTER;   //--- set root
		foreach($this->data as $value){
			switch($value['location']){
				case 'start': 	$strtmp .= '<'.$value['name'].$value['attribute'].'>'.$value['value'].ENTER;	break;
				case 'end': 	$strtmp .= '</'.$value['name'].'>'.ENTER;	break;
				default		:	$strtmp .= '<'.$value['name'].$value['attribute'].'>'.$value['value'].'</'.$value['name'].'>'.ENTER;	break;
	
			}
		} 
		$strtmp .= "</$root>".ENTER;
		$fp = fopen( $filename , "w" );
		fwrite($fp,$strtmp);
		fclose($fp);
	}
	
	/* add element
	 * @param  string name      ← element name							
	 * @param  string attribute	← element attribute										
	 * @param  string value  	← element value								
	 * @param  string location 	← element position，default after last line to add ，begin is begin element， end is end element									
	 * @param boolean return 	← true: add success，false:add failure									
	 */	
	public function add($name, $attribute, $value, $location = '')
	{
		if(!empty($name)){
			$this->data[] = array('name' => $name,'attribute' => $attribute,'value' => $value,'location' => $location);
			return true;
		}else
			return false;
	}

	/* OUT XML file
	 * @param  string filename      ← XML file name											
	 * @param  string root  		← XML root								
	 * @param  string code   		← XML code(default utf-8)
	 * return  string				← return xml file source code
	 */	
	public function outXml($filename, $root, $rootAttribute, $code='utf-8')
	{
		$strtmp  = '<?xml version="1.0" encoding="'.$code.'"?>'.ENTER;	//--- document type
		$strtmp .= "<".$root.">".ENTER;   //--- set root element
		foreach($this->data as $value){
			switch($value['location']){
				case 'begin': 	$strtmp .= '<'.$value['name'].$value['attribute'].'>'.$value['value'].ENTER;	break;
				case 'end': 	$strtmp .= '</'.$value['name'].'>'.ENTER;	break;
				default		:	$strtmp .= '<'.$value['name'].$value['attribute'].'>'.$value['value'].'</'.$value['name'].'>'.ENTER;	break;
	
			}
		} 
		$strtmp .= "</".$root.">".ENTER;
		return $strtmp;
	}	
} 
?>
 